﻿/*
 * 类名：推送考试成绩类
 * 创建：李金颖
 * 日期：2018年12月30日
 * 功能：米老师班级（戏剧影视文学、新闻学）学生三次考试成绩推送
 * 说明:根据不同班级,不同考试次数,进行查询、推送
 * 借鉴："显示成绩frmDisplayScore""练习推送frmQRcode"
 */

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BLL;
using Model;


namespace NCRE学生考试端V1._0.教师端操作类
{
    public partial class frmExamScore : Form
    {
        public frmExamScore()
        {
            InitializeComponent();
        }

        #region 窗体单一显示-李金颖-2018-12-30
        private static frmExamScore FrmExamScore = null;
        public static frmExamScore GetInstance()
        {
            if (FrmExamScore == null || FrmExamScore.IsDisposed)
            {
                FrmExamScore = new frmExamScore();
            }
            return FrmExamScore;


        }

        #endregion


        #region 防止ComboBox下拉框多次加载--刁琳童--2019年1月6日
        private void frmExamScore_FormClosing(object sender, FormClosingEventArgs e)
        {
           // FrmExamScore = null;
        }
        #endregion

        //定义一个学生分数的逻辑类
        StudentScoreBLL studentscorebll = new StudentScoreBLL();
        //定义一个学生的实体用来传递学生信息
        StudentInfoEntity studentinfo = new StudentInfoEntity();

        #region 推送一级考试成绩-李金颖-2018年12月30日
        /// <summary>
        /// 推送一级考试成绩-李金颖-2018年12月30日
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSend_Click(object sender, EventArgs e)
        {
            //根据时间、专业、班级、推送属性为1的数据，进行推送
            //推送完毕，更新推送属性为0

            //传输参数
            ExamScore examscore = new ExamScore();
            examscore.courseId = "04101001";//课程
            examscore.courseName = "计算机基础";

            //查询条件获取（学院、专业、时间）
            switch (cboCollege.Text)
            {
                case "文学院":
                    studentinfo.CollegeID = "01";
                    break;
                case "社会发展学院":
                    studentinfo.CollegeID = "02";
                    break;
                case "外国语学院":
                    studentinfo.CollegeID = "03";
                    break;
                case "管理学院":
                    studentinfo.CollegeID = "04";
                    break;
                case "美术学院":
                    studentinfo.CollegeID = "05";
                    break;
                case "音乐学院":
                    studentinfo.CollegeID = "06";
                    break;
                case "教育学院":
                    studentinfo.CollegeID = "07";
                    break;
                case "电子信息工程学院":
                    studentinfo.CollegeID = "08";
                    break;
                case "理学院":
                    studentinfo.CollegeID = "09";
                    break;
                case "化学与材料科学学院":
                    studentinfo.CollegeID = "10";
                    break;
                case "生命科学学院":
                    studentinfo.CollegeID = "11";
                    break;
                case "经济学院":
                    studentinfo.CollegeID = "12";
                    break;
                case "建筑工程学院":
                    studentinfo.CollegeID = "13";
                    break;
                case "体育学院":
                    studentinfo.CollegeID = "14";
                    break;
                case "传媒系":
                    studentinfo.CollegeID = "15";
                    break;
                case "其他":
                    studentinfo.CollegeID = "16";
                    break;
                default:
                    studentinfo.CollegeID = "01";
                    break;
            }
            studentinfo.major = cboMajor.Text.Trim();
            string starttime = dateTimePicker1.Value.ToString("yyyy-MM-dd HH:mm:ss");
            string endtime = dateTimePicker2.Value.ToString("yyyy-MM-dd HH:mm:ss");
            //查询未推送的数据
            StudentScoreBLL studentscorebll = new StudentScoreBLL();
            DataTable dtscore = null;
            dtscore = studentscorebll.IsSend(studentinfo, starttime, endtime);
            if (dtscore.Rows.Count > 0)
            {
                //传输参数
                examscore.professionName = cboMajor.Text.Trim();//专业
                examscore.institutionID = studentinfo.CollegeID;//学院
                examscore.institutionName = cboCollege.Text.Trim();

                #region 循环推送表数据
                for (int i = 0; i < dtscore.Rows.Count; i++)
                {
                    examscore.studentCode = Convert.ToString(dtscore.Rows[i]["studentID"]);//学号
                    examscore.studentName = Convert.ToString(dtscore.Rows[i]["studentName"]);//姓名
                    examscore.paperScore = Convert.ToString(dtscore.Rows[i]["score"]);//成绩
                    examscore.className = Convert.ToString(dtscore.Rows[i]["majorClass"]);//班级名称
                    string dttime = Convert.ToString(dtscore.Rows[i]["timeSpan"]);
                    examscore.examTime = DateTime.ParseExact(dttime, "yy/M/d H:mm:ss", null).ToString("yyyy-MM-dd HH:mm:ss");

                    //反序列化
                    string restult = new Resful.EndSerialize().ExamScoreSerialize(examscore);
                    restult = "[" + restult + "]";
                    string sendexamscore = IRestful.ExamScoreRestful(restult);
                    #region 循环更新推送属性、推送时间
                    string stuID = Convert.ToString(dtscore.Rows[i]["studentID"]);
                    string timeSpan = Convert.ToString(dtscore.Rows[i]["timeSpan"]);
                    string uptime = DateTime.Now.ToString();
                    studentscorebll.UpSend(stuID, timeSpan, uptime);
                    #endregion
                }
                #endregion

                MessageBox.Show("推送成功！");
            }
            else
            {
                MessageBox.Show("没有需要推送的消息！");
            }

            //更新表数据
        }
        #endregion

        #region 窗体加载事件-李金颖-2018年12月30日
        /// <summary>
        /// 窗体加载事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        DataTable dtcollege = null;
        private void frmExamScore_Load(object sender, EventArgs e)
        {
            #region 设置开始时间提前两小时，截至时间晚两小时
            dateTimePicker1.Value = DateTime.Now.AddHours(-2);
            dateTimePicker2.Value = DateTime.Now.AddHours(2);
            #endregion

            #region 下拉框信息加载
            //调用方法选择所有的专业
            dtcollege = studentscorebll.SelectAllCollege();
            //为专业的combobox控件赋值
            cboCollege.DataSource = dtcollege;
            cboCollege.DisplayMember = "学院";
            cboCollege.ValueMember = "collegeName";

            //将下拉框中的值传进studentinfo中
            studentinfo.major = "汉语言文学";
            #endregion

 
        }
        #endregion

        #region 在选择学院的时候选择该专业下的所有学生-李金颖-2019年1月2日
        /// <summary>
        /// 在选择学院的时候选择该专业下的所有学生
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cboCollege_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (cboCollege.Text)
            {
                case "文学院":
                    studentinfo.CollegeID = "01";
                    break;
                case "社会发展学院":
                    studentinfo.CollegeID = "02";
                    break;
                case "外国语学院":
                    studentinfo.CollegeID = "03";
                    break;
                case "管理学院":
                    studentinfo.CollegeID = "04";
                    break;
                case "美术学院":
                    studentinfo.CollegeID = "05";
                    break;
                case "音乐学院":
                    studentinfo.CollegeID = "06";
                    break;
                case "教育学院":
                    studentinfo.CollegeID = "07";
                    break;
                case "电子信息工程学院":
                    studentinfo.CollegeID = "08";
                    break;
                case "理学院":
                    studentinfo.CollegeID = "09";
                    break;
                case "化学与材料科学学院":
                    studentinfo.CollegeID = "10";
                    break;
                case "生命科学学院":
                    studentinfo.CollegeID = "11";
                    break;
                case "经济学院":
                    studentinfo.CollegeID = "12";
                    break;
                case "建筑工程学院":
                    studentinfo.CollegeID = "13";
                    break;
                case "体育学院":
                    studentinfo.CollegeID = "14";
                    break;
                case "传媒系":
                    studentinfo.CollegeID = "15";
                    break;
                case "其他":
                    studentinfo.CollegeID = "16";
                    break;
                default:
                    studentinfo.CollegeID = "01";
                    break;
            }

            //调用方法选择所有的专业
            DataTable dtmajor = studentscorebll.SelectMajorByCollegeID(studentinfo);
            //为专业的combobox控件赋值
            cboMajor.DataSource = dtmajor;
            cboMajor.DisplayMember = "专业";
            cboMajor.ValueMember = "major";

            if (dtmajor.Rows.Count == 0)
            {
                MessageBox.Show("该学院无数据", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                while (this.DGView.Rows.Count != 0)
                {
                    this.DGView.Rows.RemoveAt(0);
                }
            }
        }
        #endregion

        #region 显示查询到的所有学生信息-李金颖-2019年1月2日
        /// <summary>
        /// 在选择专业的时候选择该专业下的所有学生
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param> 
        private void btnInqueryScore_Click(object sender, EventArgs e)
        {
            DGView.DataSource = null;//清空数据
            //将下拉框中的值传进studentinfo中
            studentinfo.major = cboMajor.Text.Trim();
            switch (cboCollege.Text)
            {
                case "文学院":
                    studentinfo.CollegeID = "01";
                    break;
                case "社会发展学院":
                    studentinfo.CollegeID = "02";
                    break;
                case "外国语学院":
                    studentinfo.CollegeID = "03";
                    break;
                case "管理学院":
                    studentinfo.CollegeID = "04";
                    break;
                case "美术学院":
                    studentinfo.CollegeID = "05";
                    break;
                case "音乐学院":
                    studentinfo.CollegeID = "06";
                    break;
                case "教育学院":
                    studentinfo.CollegeID = "07";
                    break;
                case "电子信息工程学院":
                    studentinfo.CollegeID = "08";
                    break;
                case "理学院":
                    studentinfo.CollegeID = "09";
                    break;
                case "化学与材料科学学院":
                    studentinfo.CollegeID = "10";
                    break;
                case "生命科学学院":
                    studentinfo.CollegeID = "11";
                    break;
                case "经济学院":
                    studentinfo.CollegeID = "12";
                    break;
                case "建筑工程学院":
                    studentinfo.CollegeID = "13";
                    break;
                case "体育学院":
                    studentinfo.CollegeID = "14";
                    break;
                case "传媒系":
                    studentinfo.CollegeID = "15";
                    break;
                case "其他":
                    studentinfo.CollegeID = "16";
                    break;
                default:
                    studentinfo.CollegeID = "01";
                    break;
            }

            string starttime = dateTimePicker1.Value.ToString("yyyy-MM-dd HH:mm:ss");
            string endtime = dateTimePicker2.Value.ToString("yyyy-MM-dd HH:mm:ss");
            DataTable datasource = studentscorebll.SendExamScore(studentinfo, starttime, endtime);

            DGView.DataSource = datasource;
            DGView.Columns[0].HeaderText = "学号";
            DGView.Columns[1].HeaderText = "姓名";
            DGView.Columns[2].HeaderText = "总分";
            DGView.Columns[3].HeaderText = "专业";
            DGView.Columns[4].HeaderText = "班级";
            DGView.Columns[5].HeaderText = "年级";
            DGView.Columns[6].HeaderText = "考试时间";
            DGView.Columns[7].HeaderText = "是否已推送";
            DGView.Columns[8].HeaderText = "推送时间";
            //表格宽度自动调整
            DGView.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
        }
        #endregion

        #region 获取考试考生数据到ExamScore表-李金颖-2019年1月4日

        private void btnGetExamInfo_Click(object sender, EventArgs e)
        {
            #region 考试模式下，将考生数据存到数据库ExamScore表-李金颖-2019年1月3日
            //根据学院、专业、时间，查询考生的考试数据
            string collegeID;
            switch (cboCollege.Text)
            {
                case "文学院":
                    collegeID = "01";
                    break;
                case "社会发展学院":
                    collegeID = "02";
                    break;
                case "外国语学院":
                    collegeID = "03";
                    break;
                case "管理学院":
                    collegeID = "04";
                    break;
                case "美术学院":
                    collegeID = "05";
                    break;
                case "音乐学院":
                    collegeID = "06";
                    break;
                case "教育学院":
                    collegeID = "07";
                    break;
                case "电子信息工程学院":
                    collegeID = "08";
                    break;
                case "理学院":
                    collegeID = "09";
                    break;
                case "化学与材料科学学院":
                    collegeID = "10";
                    break;
                case "生命科学学院":
                    collegeID = "11";
                    break;
                case "经济学院":
                    collegeID = "12";
                    break;
                case "建筑工程学院":
                    collegeID = "13";
                    break;
                case "体育学院":
                    collegeID = "14";
                    break;
                case "传媒系":
                    collegeID = "15";
                    break;
                case "其他":
                    collegeID = "16";
                    break;
                default:
                    collegeID = "01";
                    break;
            }
            string major = cboMajor.Text.Trim();
            string starttime = dateTimePicker1.Value.ToString("yyyy-MM-dd HH:mm:ss");
            string endtime = dateTimePicker2.Value.ToString("yyyy-MM-dd HH:mm:ss");

            StudentScoreBLL stuScorebll = new StudentScoreBLL();
            DataTable dtsel = stuScorebll.selScore(collegeID, major, starttime, endtime);
            if (dtsel.Rows.Count > 0)
            {
                for (int i = 0; i < dtsel.Rows.Count; i++)
                {
                    //将查询到的数据插入到ExamScore表
                    StudentInfoEntity stuInfo = new StudentInfoEntity();
                    stuInfo.studentID = Convert.ToString(dtsel.Rows[i]["studentID"]);
                    stuInfo.studentName = Convert.ToString(dtsel.Rows[i]["studentName"]);
                    stuInfo.CollegeID = Convert.ToString(dtsel.Rows[i]["CollegeID"]);
                    stuInfo.major = Convert.ToString(dtsel.Rows[i]["major"]);
                    stuInfo.majorClass = Convert.ToString(dtsel.Rows[i]["majorClass"]);
                    stuInfo.grade = Convert.ToString(dtsel.Rows[i]["grade"]);
                    string score = Convert.ToString(dtsel.Rows[i]["score"]);
                    string timeSpan = Convert.ToString(dtsel.Rows[i]["timeSpan"]);
                    StudentInfoBll studentinfobll = new StudentInfoBll();
                    stuScorebll.addExamScore(stuInfo, score, timeSpan);
                }
                MessageBox.Show("数据录入完成，可以单击“查询”进行查看了哟~");
            }
            else
            {
                MessageBox.Show("当前条件下，没有考生进行考试哟~");
            }
            #endregion
        }


        #endregion
    }
}
